package com.qiguliuxing.dts.service.base;

import com.github.pagehelper.PageHelper;
import com.qiguliuxing.dts.db.dao.DtsSystemMapper;
import com.qiguliuxing.dts.db.domain.DtsSystem;
import com.qiguliuxing.dts.db.domain.DtsSystemExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DtsSystemConfigService {

	@Resource
	private DtsSystemMapper systemMapper;

	public List<DtsSystem> queryAll() {
		DtsSystemExample example = new DtsSystemExample();
		example.or();
		return systemMapper.selectByExample(example);
	}

	public List<DtsSystem> querySelective(String key, String keyWord, Integer page, Integer limit, String sort, String order) {
		DtsSystemExample example = new DtsSystemExample();
		DtsSystemExample.Criteria criteria = example.createCriteria();

		if (!StringUtils.isEmpty(key)) {
			criteria.andKeyNameLike("%" + key+ "%");
		}
		if (!StringUtils.isEmpty(keyWord)) {
			criteria.andDescLike("%" + keyWord + "%");
		}
		criteria.andDeletedEqualTo(false);

		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			example.setOrderByClause(sort + " " + order);
		}

		PageHelper.startPage(page, limit);
		return systemMapper.selectByExample(example);
	}

	public void add(DtsSystem dtsSystem) {
		dtsSystem.setAddTime(LocalDateTime.now());
		dtsSystem.setUpdateTime(LocalDateTime.now());
		systemMapper.insertSelective(dtsSystem);
	}

	public int update(DtsSystem dtsSystem) {
		dtsSystem.setUpdateTime(LocalDateTime.now());
		return systemMapper.updateByPrimaryKeySelective(dtsSystem);
	}

	public void deleteById(Integer id) {
		systemMapper.logicalDeleteByPrimaryKey(id);
	}
}
